﻿using System.Collections.Specialized;
using System.Web.Services;
using TripLOA.WebService.Serializable;
using System.Collections.Generic;

namespace TripLOA.WebService.Rest
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>

    [WebService(Namespace = "http://triploa.cli.di.unipi.it/service/rest/", Name = "RestGeoRef")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class RestGeoRef : EndPoint
    {
        private GeoRefCore core;

        #region RestGeoRef Constructors

        public RestGeoRef()
        {
            core = GeoRefCore.Istance();
        }

        #endregion

        #region RestGeoRef methods

        public string geoCoding(NameValueCollection _REQUEST, string format)
        {
            WSGeoLoc gl;
            if (_REQUEST["location"] != null)
            {
                gl = core.geoCoding(_REQUEST["location"]);
            } else {
                gl = core.geoCoding(_REQUEST["state"], _REQUEST["city"], _REQUEST["road"]);
            }

            return Serialization.serialize(gl, typeof(WSGeoLoc), format);
        }

        public string reverseGeoCoding(NameValueCollection _REQUEST, string format) {
            string lat = _REQUEST["lat"].Replace(',', '.');
            string lng = _REQUEST["lng"].Replace(',', '.');

            //Validate parameters
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(double), value = lat });
            parameters.Add(new WSParameter() { type = typeof(double), value = lng });
            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }

            return Serialization.serialize(
                core.reverseGeoCoding(
                    double.Parse(lat, System.Globalization.CultureInfo.InvariantCulture),
                    double.Parse(lng, System.Globalization.CultureInfo.InvariantCulture)
                ),
                typeof(WSGeoLoc),
                format
            );
        }
            

        public string getGeoTrip(NameValueCollection _REQUEST, string format)
        {
            return Serialization.serialize(core.getGeoTrip(), typeof(WSGeoTrip), format);
        }

        #endregion
    }
}
